home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kdesu / process.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-08-19  |  5.1 KB  |  189 lines

  1. /* vi: ts=8 sts=4 sw=4
  2.  *
  3.  * $Id: process.h 802657 2008-04-30 08:22:54Z lunakl $
  4.  *
  5.  * This file is part of the KDE project, module kdesu.
  6.  * Copyright (C) 1999,2000 Geert Jansen <jansen@kde.org>
  7.  * 
  8.  * This is free software; you can use this library under the GNU Library 
  9.  * General Public License, version 2. See the file "COPYING.LIB" for the 
  10.  * exact licensing terms.
  11.  */
  12.  
  13. #ifndef __Process_h_Included__
  14. #define __Process_h_Included__
  15.  
  16. #include <sys/types.h>
  17.  
  18. #include <qcstring.h>
  19. #include <qstring.h>
  20. #include <qstringlist.h>
  21. #include <qvaluelist.h>
  22.  
  23. #include <kdelibs_export.h>
  24.  
  25. class PTY;
  26. typedef QValueList<QCString> QCStringList;
  27.  
  28. /**
  29.  * Synchronous communication with tty programs.
  30.  *
  31.  * PtyProcess provides synchronous communication with tty based programs. 
  32.  * The communications channel used is a pseudo tty (as opposed to a pipe) 
  33.  * This means that programs which require a terminal will work.
  34.  */
  35.  
  36. class KDESU_EXPORT PtyProcess
  37. {
  38. public:
  39.     PtyProcess();
  40.     virtual ~PtyProcess();
  41.  
  42.     /**
  43.      * Forks off and execute a command. The command's standard in and output 
  44.      * are connected to the pseudo tty. They are accessible with readLine 
  45.      * and writeLine.
  46.      * @param command The command to execute.
  47.      * @param args The arguments to the command.
  48.      */
  49.     int exec(const QCString &command, const QCStringList &args);
  50.  
  51.     /**
  52.      * Reads a line from the program's standard out. Depending on the @em block 
  53.      * parameter, this call blocks until a single, full line is read. 
  54.      * @param block Block until a full line is read?
  55.      * @return The output string.
  56.      */
  57.     QCString readLine(bool block=true);
  58.     /**
  59.      * Read all available output from the program's standard out.
  60.      * @param block If no output is in the buffer, should the function block
  61.      * @return The output.
  62.      */
  63.     QCString readAll(bool block=true);
  64.  
  65.     /**
  66.      * Writes a line of text to the program's standard in.
  67.      * @param line The text to write.
  68.      * @param addNewline Adds a '\n' to the line.
  69.      */
  70.     void writeLine(const QCString &line, bool addNewline=true);
  71.  
  72.     /**
  73.      * Puts back a line of input.
  74.      * @param line The line to put back.
  75.      * @param addNewline Adds a '\n' to the line.
  76.      */
  77.     void unreadLine(const QCString &line, bool addNewline=true);
  78.  
  79.     /**
  80.      * Sets the exit string. If a line of program output matches this,
  81.      * waitForChild() will terminate the program and return.
  82.      */
  83.     void setExitString(const QCString &exit) { m_Exit = exit; }
  84.  
  85.     /**
  86.      * Waits for the child to exit. See also setExitString.
  87.      */
  88.     int waitForChild();
  89.  
  90.     /**
  91.      * Waits until the pty has cleared the ECHO flag. This is useful 
  92.      * when programs write a password prompt before they disable ECHO.
  93.      * Disabling it might flush any input that was written.
  94.      */
  95.     int WaitSlave();
  96.  
  97.     /**
  98.      * Enables/disables local echo on the pseudo tty.
  99.      */
  100.     int enableLocalEcho(bool enable=true);
  101.  
  102.     /**
  103.      * Enables/disables terminal output. Relevant only to some subclasses.
  104.      */
  105.     void setTerminal(bool terminal) { m_bTerminal = terminal; }
  106.  
  107.     /**
  108.      * Overwrites the password as soon as it is used. Relevant only to
  109.      * some subclasses.
  110.      */
  111.     void setErase(bool erase) { m_bErase = erase; }
  112.  
  113.     /**
  114.      * Set additinal environment variables.
  115.      */
  116.     void setEnvironment( const QCStringList &env );
  117.  
  118.     /**
  119.      * Returns the filedescriptor of the process.
  120.      */
  121.     int fd() {return m_Fd;}
  122.  
  123.     /**
  124.      * Returns the pid of the process.
  125.      */
  126.     int pid() {return m_Pid;}
  127.  
  128. public: /* static */
  129.     /*
  130.     ** This is a collection of static functions that can be
  131.     ** used for process control inside kdesu. I'd suggest 
  132.     ** against using this publicly. There are probably 
  133.     ** nicer Qt based ways to do what you want.
  134.     */
  135.  
  136.     /**
  137.     ** Wait @p ms miliseconds (ie. 1/10th of a second is 100ms),
  138.     ** using @p fd as a filedescriptor to wait on. Returns
  139.     ** select(2)'s result, which is -1 on error, 0 on timeout,
  140.     ** or positive if there is data on one of the selected fd's.
  141.     **
  142.     ** @p ms must be in the range 0..999 (ie. the maximum wait
  143.     ** duration is 999ms, almost one second).
  144.     */
  145.     static int waitMS(int fd,int ms);
  146.  
  147.  
  148.     /**
  149.     ** Basic check for the existence of @p pid.
  150.     ** Returns true iff @p pid is an extant process,
  151.     ** (one you could kill - see man kill(2) for signal 0).
  152.     */
  153.     static bool checkPid(pid_t pid);
  154.  
  155.     /**
  156.     ** Check process exit status for process @p pid.
  157.     ** On error (no child, no exit), return -1.
  158.     ** If child @p pid has exited, return its exit status,
  159.     ** (which may be zero).
  160.     ** If child @p has not exited, return -2.
  161.     */
  162.     enum checkPidStatus { Error=-1, NotExited=-2, Killed=-3 } ;
  163.     static int checkPidExited(pid_t pid);
  164.  
  165.  
  166. protected:
  167.     const QCStringList& environment() const;
  168.  
  169.     bool m_bErase, m_bTerminal;
  170.     int m_Pid, m_Fd;
  171.     QCString m_Command, m_Exit;
  172.  
  173. private:
  174.     int init();
  175.     int SetupTTY(int fd);
  176.  
  177.     PTY *m_pPTY;
  178.     QCString m_Inbuf, m_TTY;
  179.  
  180. protected:
  181.     virtual void virtual_hook( int id, void* data );
  182. private:
  183.     class PtyProcessPrivate;
  184.     PtyProcessPrivate *d;
  185. };
  186.  
  187.  
  188. #endif
  189.